package com.hy.mybatisplus.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;


/**
 * 描述:
 * author: hy
 * date: 2020-09-28
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class SearchRequest<T> {

    private static final String SPLIT_CHAR = ",|;";
    private static final String LIKE = "like";
    private static final String LIST = "list";
    private static final String STRING = "String";

    /**
     * 搜索值; 多个值以 SPLIT_CHAR 分割
     */
    private String search;

    /**
     * 搜索域: 多个值以 SPLIT_CHAR 分割
     */
    private String searchFields;

    /**
     * 所有域的匹配方式 like模糊匹配
     */
    private String isLike = LIKE;

    /**
     * 或者条件内容
     */
    private String equals;
    /**
     * 或者条件字段
     */
    private String equalsFileds;

    /**
     * 根据请求参数拼接查询条件
     *
     * @param entityWrapper
     */
    public void covertWrapper(QueryWrapper<T> entityWrapper) {
        if (StringUtils.isNoneBlank(this.search, this.searchFields)) {
            String[] fields = this.searchFields.split(SPLIT_CHAR);
            entityWrapper.and(w->{
                for (int i = 0; i < fields.length; i++) {
                    mergeSql(fields, i, w);
                    if (i != fields.length - 1) {
                        w.or();
                    }
                }
            });
        } else if (StringUtils.isNoneBlank(this.equals, this.equalsFileds)) {
            String[] fields = this.equalsFileds.split(SPLIT_CHAR);
            entityWrapper.and(w->{
                for (int i = 0; i < fields.length; i++) {
                    mergeEqSql(fields, i, w);
                    if (i != fields.length - 1) {
                        w.or();
                    }
                }
            });

        }
    }

    /**
     * 根据全匹配与否拼接查询条件
     *
     * @param entityWrapper
     * @param isLike
     * @param column
     * @param value
     */
    private void isLike(QueryWrapper<T> entityWrapper, String isLike, String column, String value) {
        if (LIKE.equalsIgnoreCase(isLike)) {
            entityWrapper.like(convertToUnderline(column), value);
        } else {
            entityWrapper.eq(convertToUnderline(column), value);
        }
    }


    /**
     * 给每个字段拼接查询条件
     *
     * @param fields
     * @param i
     * @param entityWrapper
     */
    private void mergeSql(String[] fields, int i, QueryWrapper<T> entityWrapper) {
        String[] strings = this.search.split(SPLIT_CHAR);
        for (int j = 0; j < strings.length; j++) {
            if (j != strings.length - 1) {
                isLike(entityWrapper, this.isLike, fields[i], strings[j]);
                entityWrapper.or();
            } else {
                isLike(entityWrapper, this.isLike, fields[i], strings[j]);
            }
        }
    }

    /**
     * 给每个字段拼接查询条件
     *
     * @param fields
     * @param i
     * @param entityWrapper
     */
    private void mergeEqSql(String[] fields, int i, QueryWrapper<T> entityWrapper) {
        String[] strings = this.equals.split(SPLIT_CHAR);
        for (int j = 0; j < strings.length; j++) {
            if (j != strings.length - 1) {
                isLike(entityWrapper, "EQUALS", fields[i], strings[j]);
                entityWrapper.or();
            } else {
                isLike(entityWrapper, "EQUALS", fields[i], strings[j]);
            }
        }
    }

    /**
     * 驼峰字符转换成下划线
     *
     * @param param
     * @return
     */
    public String convertToUnderline(String param) {
        StringBuilder sb = new StringBuilder(param);
        int temp = 0;
        for (int i = 0; i < param.length(); i++) {
            if (Character.isUpperCase(param.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toUpperCase();
    }
}
